package org.goodsl.mdd.validator;

import org.goodsl.mdd.Seg;
import org.goodsl.mdd.SingleValidResult;
import org.goodsl.mdd.Validator;

import java.math.BigDecimal;

/**
 * Created with IntelliJ IDEA.
 * User: Simon Xiao
 * Date: 12-11-13
 * Time: 下午6:52
 * To change this template use File | Settings | File Templates.
 */
public class DigitsValidator extends Validator<Seg> {

    public DigitsValidator(String message) {
        super(message);

    }

    @Override
    public SingleValidResult<Seg> verify(Seg variable) {
        Object value = variable.getValue();
        boolean isNumber = false;
        if (Number.class.isAssignableFrom(value.getClass())) {
            isNumber = true;
        }

        
        try {
        	@SuppressWarnings("unused")
			BigDecimal bigNum = new BigDecimal(value.toString());
            isNumber = true;
        } catch (NumberFormatException nfe) {
        }
        if (isNumber)
            return new SingleValidResult<Seg>(true, "", variable, this);
        else
            return new SingleValidResult<Seg>(false, getMessage(), variable, this);

    }
}
